from rest_framework.viewsets import ReadOnlyModelViewSet
from .models import Areas
from .serializers import AreaSerializer, SubAreaSerializer
from rest_framework_extensions.cache.mixins import CacheResponseMixin


class AreaViewSet(CacheResponseMixin, ReadOnlyModelViewSet):
    """
    行政区划信息
    """
    pagination_class = None  # 行政区划信息不分页

    def get_queryset(self):

        if self.action == 'list':
            return Areas.objects.filter(parent=None)
        else:
            return Areas.objects.all()

    def get_serializer_class(self):

        if self.action == 'list':
            return AreaSerializer
        else:
            return SubAreaSerializer
